package com.example.seh.person.sell.errand;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.seh.R;
import com.example.seh.chat.ChatActivity;
import com.example.seh.home.substitution.DeliverDTO;
import com.example.seh.person.sell.second.OrderDTO;
import com.example.seh.person.sell.second.SellAdapter;

import java.util.ArrayList;
import java.util.List;

public class ErrandAdapter extends BaseAdapter {
    private Context context;
    private int item;
    private List<DeliverDTO> lists = new ArrayList<>();

    public ErrandAdapter(Context context, int item, List<DeliverDTO> lists) {
        this.context = context;
        this.item = item;
        this.lists = lists;
    }

    @Override
    public int getCount() {
        if (lists != null) {
            return lists.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (lists != null) {
            return lists.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(item, null);
            viewHolder = new ViewHolder();
            viewHolder.decs = convertView.findViewById(R.id.sell_errand_decs);
            viewHolder.goodsAddress = convertView.findViewById(R.id.sell_errand_goodsAddress);
            viewHolder.myAddress = convertView.findViewById(R.id.sell_errand_myAddress);
            viewHolder.errandState = convertView.findViewById(R.id.sell_errand_state);
            viewHolder.substitutes = convertView.findViewById(R.id.callSubstitutes);
            viewHolder.code = convertView.findViewById(R.id.sell_errand_code);
            viewHolder.time = convertView.findViewById(R.id.delivery_time);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.decs.setText(lists.get(position).getDeliverDesc());
        viewHolder.goodsAddress.setText(lists.get(position).getAddressName());
        viewHolder.myAddress.setText(lists.get(position).getReceiveName());
        viewHolder.code.setText(lists.get(position).getDeliverCode());
        viewHolder.time.setText(lists.get(position).getConfirmTime());
        if (lists.get(position).getDeliverStatus() == 1){
            viewHolder.errandState.setText("代取中");
        }else if (lists.get(position).getDeliverStatus() == 2){
            viewHolder.errandState.setText("代取完成");
        }

        viewHolder.substitutes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int buyerId = lists.get(position).getUserId();
                Log.e("bought",lists.get(position).getIssuerDTO().toString());
                Intent intent = new Intent(context, ChatActivity.class);
                intent.putExtra("user", lists.get(position).getIssuerDTO());
                context.startActivity(intent);
            }
        });

        return convertView;
    }

    class ViewHolder{
        TextView decs,goodsAddress,myAddress,substitutes,errandState,code,time;
    }

}
